package _binary_search;

/**
 * 744. 寻找比目标字母大的最小字母
 */
public class No744 {
    /**
     * 1. 闭区间
     */
    public char nextGreatestLetter1(char[] letters, char target) {
        target += 1;
        int n = letters.length;
        int left = 0, right = n - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (letters[mid] < target) left = mid + 1;
            else right = mid - 1;
        }
        return letters[left == n ? 0 : left];
    }

    /**
     * 2. 左闭右开
     */
    public char nextGreatestLetter2(char[] letters, char target) {
        target += 1;
        int n = letters.length;
        int left = 0, right = n;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (letters[mid] < target) left = mid + 1;
            else right = mid;
        }
        return letters[left == n ? 0 : left];
    }

    /**
     * 3. 开区间
     */
    public char nextGreatestLetter3(char[] letters, char target) {
        target += 1;
        int n = letters.length;
        int left = -1, right = n;
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (letters[mid] < target) left = mid;
            else right = mid;
        }
        return letters[right == n ? 0 : right];
    }
}
